package command;

import java.io.IOException;
import java.util.Properties;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Simple Json Utils
 * @author Jeff Zhuo
 *
 */
public class JsonUtils {
	private final static ObjectMapper mapper;

	static {
		mapper = new ObjectMapper();
	}

	/**
	 * Change object to Json string
	 * 
	 * @param object
	 *            the object to json
	 * @return
	 */
	public static String objectToJson(Object object) {
		try {
			return mapper.writeValueAsString(object);
		} catch (JsonProcessingException e) {
		}
		return null;
	}

	/**
	 * Get the Properties from Json
	 * 
	 * @param jsonText
	 *            the string of Json
	 * @return
	 */
	public static Properties objectFromJson(String jsonText) {
		return objectFromJson(jsonText, Properties.class);
	}

	private static Properties objectFromJson(String jsonText, Class<?> valueType) {
		try {
			return (Properties) mapper.readValue(jsonText, valueType);
		} catch (JsonParseException e) {
		} catch (JsonMappingException e) {
		} catch (IOException e) {
		}
		return null;
	}
}
